<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Module: ActiveResource::Validations</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Module</strong></td>
          <td class="class-name-in-header">ActiveResource::Validations</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../files/vendor/rails/activeresource/lib/active_resource/validations_rb.html">
                vendor/rails/activeresource/lib/active_resource/validations.rb
                </a>
        <br />
            </td>
        </tr>

        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">

    <div id="description">
      <p>
<a href="../Module.html">Module</a> to support validation and <a
href="Validations.html#M001202">errors</a> with Active Resource objects.
The module overrides <a href="Base.html#M001267">Base#save</a> to rescue
ActiveResource::ResourceInvalid exceptions and parse the <a
href="Validations.html#M001202">errors</a> returned in the web service
response. The module also adds an <tt><a
href="Validations.html#M001202">errors</a></tt> collection that mimics the
interface of the <a href="Validations.html#M001202">errors</a> provided by
<a href="../ActiveRecord/Errors.html">ActiveRecord::Errors</a>.
</p>
<h4>Example</h4>
<p>
Consider a Person resource on the server requiring both a
<tt>first_name</tt> and a <tt>last_name</tt> with a
<tt>validates_presence_of :first_name, :last_name</tt> declaration in the
model:
</p>
<pre>
  person = Person.new(:first_name =&gt; &quot;Jim&quot;, :last_name =&gt; &quot;&quot;)
  person.save                   # =&gt; false (server returns an HTTP 422 status code and errors)
  person.valid?                 # =&gt; false
  person.errors.empty?          # =&gt; false
  person.errors.count           # =&gt; 1
  person.errors.full_messages   # =&gt; [&quot;Last name can't be empty&quot;]
  person.errors.on(:last_name)  # =&gt; &quot;can't be empty&quot;
  person.last_name = &quot;Halpert&quot;
  person.save                   # =&gt; true (and person is now saved to the remote service)
</pre>

    </div>


   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M001202">errors</a>&nbsp;&nbsp;
      <a href="#M001200">save_with_validation</a>&nbsp;&nbsp;
      <a href="#M001201">valid?</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">





      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M001202" class="method-detail">
        <a name="M001202"></a>

        <div class="method-heading">
          <a href="#M001202" class="method-signature">
          <span class="method-name">errors</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns the <a href="Errors.html">Errors</a> object that holds all
information about attribute error messages.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001202-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001202-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activeresource/lib/active_resource/validations.rb, line 270</span>
270:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">errors</span>
271:       <span class="ruby-ivar">@errors</span> <span class="ruby-operator">||=</span> <span class="ruby-constant">Errors</span>.<span class="ruby-identifier">new</span>(<span class="ruby-keyword kw">self</span>)
272:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001200" class="method-detail">
        <a name="M001200"></a>

        <div class="method-heading">
          <a href="#M001200" class="method-signature">
          <span class="method-name">save_with_validation</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Validate a resource and save (POST) it to the remote web service.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001200-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001200-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activeresource/lib/active_resource/validations.rb, line 247</span>
247:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">save_with_validation</span>
248:       <span class="ruby-identifier">save_without_validation</span>
249:       <span class="ruby-keyword kw">true</span>
250:     <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">ResourceInvalid</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">error</span>
251:       <span class="ruby-identifier">errors</span>.<span class="ruby-identifier">from_xml</span>(<span class="ruby-identifier">error</span>.<span class="ruby-identifier">response</span>.<span class="ruby-identifier">body</span>)
252:       <span class="ruby-keyword kw">false</span>
253:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001201" class="method-detail">
        <a name="M001201"></a>

        <div class="method-heading">
          <a href="#M001201" class="method-signature">
          <span class="method-name">valid?</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Checks for <a href="Validations.html#M001202">errors</a> on an object
(i.e., is resource.errors empty?).
</p>
<h4>Examples</h4>
<pre>
  my_person = Person.create(params[:person])
  my_person.valid?
  # =&gt; true

  my_person.errors.add('login', 'can not be empty') if my_person.login == ''
  my_person.valid?
  # =&gt; false
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001201-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001201-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activeresource/lib/active_resource/validations.rb, line 265</span>
265:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">valid?</span>
266:       <span class="ruby-identifier">errors</span>.<span class="ruby-identifier">empty?</span>
267:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>